package com.htudata.transctLive.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletRequest;

import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.common.service.DataEhcacheService;
import com.htudata.common.service.TokenService;
import com.htudata.common.vo.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.live.pojo.vo.AlarmsCurrentVO;
import com.htudata.live.pojo.vo.DataTypesVO;
import com.htudata.live.service.DataTypeService;
import com.htudata.live.service.TransctAlarmService;
import com.htudata.transctLive.dao.TransctLiveDao;
import com.htudata.transctLive.pojo.vo.TransctFlowGraphicDisplayVo;
import com.htudata.transctLive.service.TransctFlowService;
import com.htudata.transctLive.service.TransctLiveService;
import com.htudata.transctNode.service.TransctLinkService;
import com.htudata.transctNode.service.TransctNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransctLiveServiceImpl implements TransctLiveService {

  @Autowired
  private TransctLiveDao liveDao;
  
  @Autowired
  private DataTypeService dataTypeService;
  
  @Autowired
  private TransctFlowService flowService;
  
  @Autowired
  private TransctAlarmService alarmService;
  
  @Autowired
  private TransctNodeService nodeService;
  
  @Autowired
  private TransctLinkService linkService;
  
  @Autowired
  private DataEhcacheService dataEhcacheService;
  
  @Autowired
  private HttpServletRequest request;
  
  @Value("${virtual_station}")
  private int VIRTUALSTATION;

  @Override
  public String getDataTypes(boolean needJudgeChange, String a2) throws Exception{
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_001.getCode(), getDataTypeList(), "获取数据类型成功");
    } catch (Exception e) {
      throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    if (!needJudgeChange)
      return result; 
    Object temp = this.dataEhcacheService.getValueFromCache(RequestSeqEnum.SEQ_001.getCode());
    if (temp != null) {
      String oldJson = (String)temp;
      if (oldJson.equals(result))
        return null; 
    } 
    this.dataEhcacheService.putValueToCache(RequestSeqEnum.SEQ_001.getCode(), result);
    return result;
  }

  @Override
  public JSONObject getDataTypeList()throws Exception {
    JSONObject dataType = new JSONObject();
    AtomicInteger totalItem = new AtomicInteger();
    List<DataTypesVO> dataTypeVOList = this.dataTypeService.getVOList(new String[0]);
    dataTypeVOList.forEach(dataTypeVO -> totalItem.set(totalItem.get() + dataTypeVO.getItemNum()));
    dataType.put("dataTypes", dataTypeVOList);
    dataType.put("totalItem", totalItem);
    dataType.put("play_interval", this.dataTypeService.selectGlobalConf().get("play_interval"));
    return dataType;
  }
  @Override
  public List<AlarmsCurrentVO> getAlarmList() throws Exception{
    List<AlarmsCurrentVO> alramCurrentVOList = this.alarmService.getVOList(new String[0]);
    return alramCurrentVOList;
  }
  @Override
  public ResponseResult get3DFlows(String interfaceSeq, boolean needJudgeChange, String flowId, int alarm, int statis, String a2, long userId) throws Exception{
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(interfaceSeq, 
          get3DFlowList(flowId, alarm, statis, a2, userId), "获取事务流程图数据成功");
    } catch (Exception e) {
      throw e;
    } 
    return result;
  }

  @Override
  public List<TransctFlowGraphicDisplayVo> get3DFlowList(String flowId, int alarm, int statis, String a2, long userId) throws Exception{
    return this.flowService.getFlowGraphicDisplayVoList(flowId, alarm, statis, a2, userId);
  }
  @Override
  public JSONObject get3DFlowList3(int alarm, int statis, int nodeTypeId, int page, int perPage, int stationTypeId, String a2)throws Exception {
    return this.flowService.getFlowGraphicDisplayVoList3(alarm, statis, nodeTypeId, page, perPage, stationTypeId, a2);
  }
  @Override
  public JSONObject get3DFlowList2(int alarm, int statis, int nodeTypeId, int page, int perPage, int stationTypeId, String a2) throws Exception{
    return this.flowService.getFlowGraphicDisplayVoList2(alarm, statis, nodeTypeId, page, perPage, stationTypeId, a2);
  }

  @Override
  public JSONObject get3DFlowListV2(int alarm, int statis, int nodeTypeId, int page, int perPage, int level, int stationTypeId, String a2) throws Exception{
    return this.flowService.getFlowGraphicDisplayVoListV2(alarm, statis, nodeTypeId, page, perPage, level, stationTypeId, a2);
  }

  @Override
  @Transactional(isolation = Isolation.SERIALIZABLE)
  public List<TransctFlowGraphicDisplayVo> get3DFlowList(String flowId, String a2) throws Exception {
    int row = this.flowService.updateFlowEditStatus(flowId, 1);
    long userId = TokenService.getUserId(this.request);
    List<TransctFlowGraphicDisplayVo> flowGraphicVoList = this.flowService.getFlowGraphicDisplayVoList(flowId, 0, 0, a2, userId);
    this.liveDao.deleteTempNodesByFlowId(flowId);
    this.linkService.deleteTempLinksByFlowId(flowId);
    this.nodeService.copyNodeToNodeTemp(flowId);
    this.linkService.copyLinkToLinkTemp(flowId);
    return flowGraphicVoList;
  }
  @Override
  public JSONObject getVirtualNodes(String nodeId, int V02001, String V01301, String stationName, String V_PRCODE, String V_CITY, String V_ADDRESS, int pageNum, int pageSize) {
    JSONObject result = new JSONObject(true);
    JSONArray virtualNodes = this.liveDao.selectVirtualNodesByNodeId(nodeId, V02001, V01301, stationName, V_PRCODE, V_CITY, V_ADDRESS, 
        getRowIndex(pageNum, pageSize), pageSize, this.VIRTUALSTATION);
    result.put("virtualNodes", virtualNodes.get(0));
    result.put("total", ((HashMap)((ArrayList<HashMap>)virtualNodes
        .get(1)).get(0)).get("total"));
    return result;
  }
  
  public static int getRowIndex(int pageNum, int pageSize) {
    return (pageNum > 0) ? ((pageNum - 1) * pageSize) : 0;
  }
}
